/*
@Time : 2019/5/31 0031 11:41
@Author : visons
*/
package base

import (
	"cellent_test/base/model"
	"cellent_test/proto"
	"gitee.com/visons/gopkg/log"
	"github.com/davyxu/cellnet"
	"github.com/davyxu/cellnet/peer"
	"github.com/davyxu/cellnet/proc"
)

func CreateCommicatePeer(param model.ServiceParam) {
	msgFunc := proto.GetMessageHandler()

	queue := cellnet.NewEventQueue()

	p := peer.NewGenericPeer(param.NetPeerType, param.SvcName, param.ListenAddr, queue)

	proc.BindProcessorHandler(p, param.NetProcName, func(ev cellnet.Event) {
		switch ev.Message().(type) {
		case *cellnet.SessionConnected:
			log.Infof("session %d connect", ev.Session().ID())
		case *cellnet.SessionClosed:
			log.Infof("session %d closed", ev.Session().ID())
		default:
			if msgFunc != nil {
				msgFunc(ev)
			}
		}

	})

	p.Start()
	queue.StartLoop()
	queue.Wait()
}
